Grokking JavaScript Fundamentals
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who should take this course?
Course Overview
Introduction to JavaScript
JavaScript Basics
JavaScript - Hello World
JavaScript - Comments
Quiz
JavaScript Variables and Operators
JavaScript - Variables
JavaScript - Operators
Quiz
JavaScript Conditionals
Introduction to Conditional Statements in JavaScript
JavaScript - If...Else
JavaScript - Switch Case
Quiz
JavaScript Objects
Introduction to Objects in JavaScript
JavaScript - Number
JavaScript - Boolean
JavaScript - Math
JavaScript - Date
Quiz
Introductions to Function in JavaScript
JavaScript - Variable Scope
JavaScript - Default Parameters
JavaScript - Arrow Functions
Quiz
Introduction to Strings in JavaScript
JavaScript - String Methods
Introduction to Array in JavaScript
JavaScript - Array Methods
Quiz
Introduction to loops in JavaScript
JavaScript - for loop
JavaScript - while loop
JavaScript - do-while loop
JavaScript - for-in loop
JavaScript - for-of loop
JavaScript - Loop Control
Quiz
JavaScript - Maps
JavaScript - Sets
Quiz
Where to Go from Here
JavaScript - Math

The Math object in JavaScript provides a collection of properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All its properties and methods are static, meaning you access them using the Math prefix, not by creating a Math object instance. This object is extremely useful for performing mathematical tasks, ranging from basic arithmetic operations to complex calculations.

Syntax

Since Math is a static object, you use it directly without instantiation:

let result = Math.methodName(parameters);
  • methodName represents the Math method you wish to use.
  • parameters are the arguments that the method accepts.

Properties

The Math object includes properties for mathematical constants:

PropertyDescription
Math.EEuler's constant and the base of natural logarithms, approximately 2.718.
Math.LN2Natural logarithm of 2, approximately 0.693.
Math.LN10Natural logarithm of 10, approximately 2.303.
Math.LOG2EBase 2 logarithm of E, approximately 1.443.
Math.LOG10EBase 10 logarithm of E, approximately 0.434.
Math.PIRatio of the circumference of a circle to its diameter, approximately 3.14159.
Math.SQRT1_2Square root of 1/2, approximately 0.707.
Math.SQRT2Square root of 2, approximately 1.414.

Methods

Math object methods for performing various mathematical operations:

MethodDescription
Math.abs(x)Returns the absolute value of x.
Math.acos(x)Returns the arccosine of x, in radians.
Math.asin(x)Returns the arcsine of x, in radians.
Math.atan(x)Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians.
Math.atan2(y, x)Returns the arctangent of the quotient of its arguments.
Math.ceil(x)Returns the smallest integer greater than or equal to x.
Math.cos(x)Returns the cosine of x (x is in radians).
Math.exp(x)Returns Ex, where x is the argument, and E is Euler's constant (2.718...), the base of the natural logarithms.
Math.floor(x)Returns the largest integer less than or equal to x.
Math.log(x)Returns the natural logarithm (base E) of x.
Math.max(x, y, z, ...)Returns the number with the highest value.
Math.min(x, y, z, ...)Returns the number with the lowest value.
Math.pow(x, y)Returns the value of x to the power of y.
Math.random()Returns a random number between 0 (inclusive) and 1 (exclusive).
Math.round(x)Rounds x to the nearest integer.
Math.sin(x)Returns the sine of x (x is in radians).
Math.sqrt(x)Returns the square root of x.
Math.tan(x)Returns the tangent of an angle.

Examples

Example 1: Calculating the Area of a Circle

Javascript

. . . .
  • This calculates the area of a circle using Math.PI and Math.pow() for the radius squared.

Example 2: Generating a Random Number

Javascript

. . . .
  • Generates a random number between 0 (inclusive) and 1 (exclusive).

Example 3: Finding the Maximum and Minimum in a List of Numbers

Javascript

. . . .
  • Finds the largest and smallest numbers in a list using Math.max() and Math.min().

Example 4: Rounding a Number

Javascript

. . . .
  • Rounds a number to the nearest integer using Math.round().
JavaScript - Boolean
JavaScript - Date
Mark as Completed

On this page

Examples